import os
from utils import functions, k_util, price_util, parse_util, date_util
from service.data_service import stock_dict

base_path = os.getcwd().split('\\StockMonitor')[0] + '\\StockMonitor\\'
stock_path = base_path.split('\\stock')[0] + '\\stock\\'

# 一阳穿三线的选股公式
# m5:=ma(c,5); 收盘
# m10:=ma(c,10);
# m30:=ma(c,30);
# rc:=ref(c,1);  日线昨天收盘价格
# f1:=c>m5 and rc<ref(m5,1);
# f2:=c>m10 and rc<ref(m10,1);
# f3:=c>m30 and rc<ref(m30,1);
# f1 and f2 and f3;

date_str = date_util.get_date_str()


def select_all():
    result_path = stock_path + '\\result\\一阳三线\\' + date_str
    if not os.path.exists(result_path):
        os.makedirs(result_path)
    result_file = open(result_path + '\\筛选结果' + date_str, mode='w', encoding='utf-8')
    for key in stock_dict:
        if key[0:3] == '300' or key[0:3] == '688':
            continue
        main_select(key, result_file)
    result_file.close()


def main_select(code, result_file):
    klines = k_util.day_k_just_local(code, 33)
    m1 = parse_util.p_float(functions.MAK(code, 1, klines))
    m5 = parse_util.p_float(functions.MAK(code, 5, klines))
    if m1 > m5: return
    m10 = parse_util.p_float(functions.MAK(code, 10, klines))
    if m1 > m10: return
    m20 = parse_util.p_float(functions.MAK(code, 20, klines))
    if m1 > m20: return
    m30 = parse_util.p_float(functions.MAK(code, 30, klines))
    c = parse_util.p_float(price_util.get_rt_p(code))
    if (c > m5) and (c > m10) and (c > m20):
        stock_info = stock_dict[code]
        stock_info['m1'] = m1
        stock_info['m5'] = m5
        stock_info['m10'] = m10
        stock_info['m20'] = m20
        if c > m30 and m1 < m30:
            stock_info['m30'] = m30
        result_file.write(str(stock_info) + '\n')
        print(code + "一线穿三阳一三三三一三三三一三三三一三三三一三三三一三三三一三三三")


if __name__ == '__main__':
    print("一阳穿三线")
    # sto_info = select_util.select_doc()
    # for key in sto_info:
    #     main_select(key)
    select_all()
    # main_select('605108')
